import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    // 反射创建对象
    public static void reflectNewInstance() {
        try {
            // 获取Class对象
            Class<?> c1 = Class.forName("Student");
            // 创建类的实例
            Student student = (Student)c1.newInstance();
            System.out.println("学生对象"+student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            // 获取Class对象
            Class<?> c1 = Class.forName("Student");
            // 获取私有的构造方法
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);
            // 确认要调用private
            constructor.setAccessible(true);
            // 用私有的构造方法 创建对象
            Student student = (Student)constructor.newInstance("aa",11);

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有属性
    public static void reflectPrivateField() {
        try {
            // 获取Class对象
            Class<?> c1 = Class.forName("Student");

            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            // 发射创建一个对象
            Student student = (Student)c1.newInstance();
            // 修改这个对象的私有属性
            field.set(student, "haha");

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod() {
        try {
            // 获取Class对象
            Class<?> c1 = Class.forName("Student");

            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);

            Student student = (Student)c1.newInstance();

            method.invoke(student, "调用私有方法");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectNewInstance();
        reflectPrivateConstructor();
        reflectPrivateField();
        reflectPrivateMethod();
    }
}
